package com.auto.learning.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.bean.UserInfoBean;
import com.auto.learning.event.Handle;
import com.auto.learning.net.Cache;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.SharedPreferencesUtils;

public class ChangeLoginNameActivity extends Activity{
	
	private LinearLayout activity_base_back,activity_base_more;
	private TextView tv_ok;
	private EditText et_loginname;
	private UserInfoBean userInfoBean;
	private ImageView image_clear;
	
	private Handle handle = new Handle(this){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case HttpRequestHelper.GETDATA_SUCCESS:
				CommonMethod.makeNotice(ChangeLoginNameActivity.this, "修改成功");
				userInfoBean.setLoginName(et_loginname.getText().toString());
				MyUserInfo.getInstance().setUserInfoBean(userInfoBean);
				Cache.cacheUserInfo2Local(userInfoBean);
				setResult(202);
				finish();
				break;
			case HttpRequestHelper.GETDATA_FAIL:
				if (msg.obj != null) {
					CommonMethod.makeNotice(ChangeLoginNameActivity.this, "" + msg.obj.toString());
				}
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_loginname_activity);
		initView();
		TemporaryVariable.activities.push(this);
	}
	
	private void initView(){
		userInfoBean = MyUserInfo.getInstance().getUserInfoBean();
		activity_base_back = (LinearLayout) findViewById(R.id.activity_base_back);
		activity_base_more = (LinearLayout) findViewById(R.id.activity_base_more);
		image_clear = (ImageView) findViewById(R.id.image_clear);
		image_clear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				et_loginname.setText("");
				et_loginname.requestFocus();
				activity_base_more.setEnabled(true);
			}
		});
		activity_base_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		tv_ok = (TextView) findViewById(R.id.tv_ok);
		activity_base_more.setEnabled(false);
		activity_base_more.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String loginName = et_loginname.getText().toString();
				if (TextUtils.isEmpty(loginName.trim())) {
					CommonMethod.makeNotice(ChangeLoginNameActivity.this, "用户昵称不能空");
					return;
				} else if (loginName.trim().length() > 10) {
					CommonMethod.makeNotice(ChangeLoginNameActivity.this, "用户昵称不能超过10个字符");
					return;
				}
				changeLoginName(loginName.trim());
			}
		});
		et_loginname = (EditText) findViewById(R.id.et_loginname);
		et_loginname.setText(userInfoBean.getLoginName());
		et_loginname.setFocusable(true);
		et_loginname.setSelection(!TextUtils.isEmpty(userInfoBean.getLoginName()) ? userInfoBean.getLoginName().length() : 0);
		et_loginname.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (userInfoBean.getLoginName().equals(et_loginname.getText().toString()) || "".equals(et_loginname.getText().toString())) {
					activity_base_more.setEnabled(false);
				}else {
					activity_base_more.setEnabled(true);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
	}
	
	public void changeLoginName(String name){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(SharedPreferencesUtils.getParam(this, HttpRequestHelper.USERID, 0))));
		params.add(new BasicNameValuePair("name", name));
		HttpRequestHelper.executeHttpData2(this, HttpRequestHelper.NICK_NAME_CHANGE, params, handle, HttpRequestHelper.GETDATA_SUCCESS,HttpRequestHelper.GETDATA_FAIL);
	
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		TemporaryVariable.activities.removeElement(this);
		super.onDestroy();
	}
}
